package com.xywh.dakaqiandao.face;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.baidu.idl.face.platform.FaceConfig;
import com.baidu.idl.face.platform.FaceSDKManager;
import com.baidu.idl.face.platform.FaceStatusEnum;
import com.baidu.idl.face.platform.LivenessTypeEnum;
import com.baidu.idl.face.platform.ui.FaceLivenessActivity;
import com.jess.arms.utils.LogUtils;
import com.jess.arms.utils.SharedPreferencesHelper;
import com.xywh.dakaqiandao.app.ChuRuKuApplication;
import com.xywh.dakaqiandao.face.widget.DefaultDialog;

import org.simple.eventbus.EventBus;

import java.util.HashMap;
import java.util.Random;

public class FaceLivenessExpActivity extends FaceLivenessActivity {

    protected SharedPreferencesHelper mSharedPreferencesHelper;
    private DefaultDialog mDefaultDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSharedPreferencesHelper = SharedPreferencesHelper.getInstance(this);

//        FaceConfig config = FaceSDKManager.getInstance().getFaceConfig();
//        // SDK初始化已经设置完默认参数（推荐参数），您也根据实际需求进行数值调整
//        ChuRuKuApplication.livenessList.clear();
//        int pick1 = new Random().nextInt(LivenessTypeEnum.values().length);
//        int pick2 = new Random().nextInt(LivenessTypeEnum.values().length);
//        int pick3 = new Random().nextInt(LivenessTypeEnum.values().length);
//
//        ChuRuKuApplication.livenessList.add(LivenessTypeEnum.values()[pick1]);
//        ChuRuKuApplication.livenessList.add(LivenessTypeEnum.values()[pick2]);
//        ChuRuKuApplication.livenessList.add(LivenessTypeEnum.values()[pick3]);
//
//        config.setLivenessTypeList(ChuRuKuApplication.livenessList);
//        FaceSDKManager.getInstance().setFaceConfig(config);

    }

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public void onLivenessCompletion(FaceStatusEnum status, String message, HashMap<String, String> base64ImageMap) {
        super.onLivenessCompletion(status, message, base64ImageMap);
        if (status == FaceStatusEnum.OK && mIsCompletion) {
            showMessageDialog1("人脸检测", "检测成功,确定打卡?", base64ImageMap);

        } else if (status == FaceStatusEnum.Error_DetectTimeout ||
                status == FaceStatusEnum.Error_LivenessTimeout ||
                status == FaceStatusEnum.Error_Timeout) {
            showMessageDialog("人脸检测", "采集超时");
        }
    }

    private void showMessageDialog1(String title, String message, HashMap<String, String> base64ImageMap) {
        if (mDefaultDialog == null) {
            DefaultDialog.Builder builder = new DefaultDialog.Builder(this);
            builder.setTitle(title).
                    setMessage(message).
                    setNegativeButton("确认",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    mDefaultDialog.dismiss();
                                    if (base64ImageMap != null && base64ImageMap.containsKey("bestImage0")) {
//                                        Intent intent = new Intent();

//                                        intent.putExtra("bestImage0", "111");
//                                        intent.putExtra("Eye", "222");
//                                        mSharedPreferencesHelper.putString("bestImage0", base64ImageMap.get("bestImage0"));
//                                        mSharedPreferencesHelper.putString("Eye", base64ImageMap.get("Eye"));
                                        EventBus.getDefault().post(base64ImageMap.get("bestImage0"), "bestImage0");
//                                        setResult(105, intent);
                                        finish();
                                    }
                                }
                            });
            mDefaultDialog = builder.create();
            mDefaultDialog.setCancelable(true);
        }
        mDefaultDialog.dismiss();
        mDefaultDialog.show();
    }

    private void showMessageDialog(String title, String message) {
        if (mDefaultDialog == null) {
            DefaultDialog.Builder builder = new DefaultDialog.Builder(this);
            builder.setTitle(title).
                    setMessage(message).
                    setNegativeButton("确认",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    mDefaultDialog.dismiss();
                                    Intent intent = new Intent();
                                    intent.putExtra("bestImage0", "");
                                    intent.putExtra("Eye", "");
                                    setResult(105, intent);
                                    finish();
                                }
                            });
            mDefaultDialog = builder.create();
            mDefaultDialog.setCancelable(true);
        }
        mDefaultDialog.dismiss();
        mDefaultDialog.show();
    }

}
